package com.app.oral.dto

import com.app.oral.model.*
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import java.math.BigDecimal
import java.time.LocalDateTime

/**
 * 开始练习/测试请求DTO
 */
data class StartExerciseRequest(
    val paperId: Long,
    val mode: ExerciseMode
)

/**
 * 练习进度响应DTO
 */
data class ExerciseProgressResponse(
    val recordId: String,
    val paperId: Long,
    val mode: ExerciseMode,
    val status: ExerciseStatus,
    val currentSection: ExerciseStage,
    val currentQuestionIndex: Int,
    val progressPercentage: Int,
    val totalScore: BigDecimal?,
    val createdAt: LocalDateTime,
    val updatedAt: LocalDateTime,
    val completedAt: LocalDateTime?
)

/**
 * 更新短文朗读请求DTO
 */
data class UpdateReadingRequest(
    val recordId: String,
    val audioFileId: String,
    val scoreDetails: ReadingScoreDetailsDto
)

/**
 * 短文朗读评分详情DTO - 对应腾讯云API返回格式
 */
data class ReadingScoreDetailsDto(
    val suggestedScore: BigDecimal,     // 建议评分
    val pronAccuracy: BigDecimal,       // 整体精准度
    val pronFluency: BigDecimal,        // 整体流利度
    val pronCompletion: BigDecimal,     // 整体完整度
    val wordMatches: WordMatchesDto? = null // 单词匹配分析数据
) {
    /**
     * 转换为内部模型
     */
    fun toModel(): ReadingScoreDetails {
        return ReadingScoreDetails(
            accuracy = pronAccuracy,
            fluency = pronFluency,
            completeness = pronCompletion,
            suggestedScore = suggestedScore,
            wordMatches = wordMatches?.toModel()
        )
    }
}

/**
 * 单词匹配分析DTO
 */
@JsonIgnoreProperties(ignoreUnknown = true)
data class WordMatchesDto(
    val wordAnalysis: List<WordAnalysisDto>, // 每个单词的分析结果
    val statistics: WordStatisticsDto,       // 统计信息
    val accuracy: Int                        // 准确率百分比
) {
    fun toModel(): WordMatches {
        return WordMatches(
            wordAnalysis = wordAnalysis.map { it.toModel() },
            statistics = statistics.toModel(),
            accuracy = accuracy
        )
    }
}

/**
 * 单词分析详情DTO
 */
@JsonIgnoreProperties(ignoreUnknown = true)
data class WordAnalysisDto(
    val index: Int,                      // 单词索引位置
    val word: String,                    // 识别到的单词
    val referenceWord: String,           // 参考单词
    val matchTag: Int,                   // 腾讯云原始MatchTag值
    val matchStatus: String,             // 匹配状态: correct/wrong/missing
    val isCorrect: Boolean               // 是否正确
) {
    fun toModel(): WordAnalysis {
        return WordAnalysis(
            index = index,
            word = word,
            referenceWord = referenceWord,
            matchTag = matchTag,
            matchStatus = matchStatus,
            isCorrect = isCorrect
        )
    }
}

/**
 * 单词匹配统计信息DTO
 */
@JsonIgnoreProperties(ignoreUnknown = true)
data class WordStatisticsDto(
    val total: Int,                      // 总单词数
    val correct: Int,                    // 正确单词数
    val wrong: Int,                      // 错误单词数
    val missing: Int                     // 遗漏单词数
) {
    fun toModel(): WordStatistics {
        return WordStatistics(
            total = total,
            correct = correct,
            wrong = wrong,
            missing = missing
        )
    }
}

/**
 * 话题简述(表达题)详细评分DTO
 */
@JsonIgnoreProperties(ignoreUnknown = true)
data class ExpressionScoreDetailsDto(
    val pronAccuracy: BigDecimal,       // 整体精准度
    val pronFluency: BigDecimal,        // 整体流利度
    val pronCompletion: BigDecimal,     // 整体完整度
    val suggestedScore: BigDecimal? = null // 可选：建议分
) {
    fun toModel(): ExpressionScoreDetails {
        return ExpressionScoreDetails(
            accuracy = pronAccuracy,
            fluency = pronFluency,
            completeness = pronCompletion,
            suggestedScore = suggestedScore
        )
    }
}

/**
 * 更新视听应答请求DTO
 */
data class UpdateListeningRequest(
    val recordId: String,
    val listeningIndex: Int,  // 1-5
    val audioFileId: String,
    val score: BigDecimal
)

/**
 * 更新情景问答请求DTO
 */
data class UpdateDialogueRequest(
    val recordId: String,
    val dialogueIndex: Int,  // 1-5
    val audioFileId: String,
    val score: BigDecimal
)

/**
 * 更新话题简述请求DTO
 */
data class UpdateExpressionRequest(
    val recordId: String,
    val audioFileId: String,
    val score: BigDecimal,
    val scoreDetails: ExpressionScoreDetailsDto
)

/**
 * 修改话题简述分数请求DTO（不包含音频文件）
 */
data class UpdateExpressionScoreRequest(
    val recordId: String,
    val score: BigDecimal,
    val scoreDetails: ExpressionScoreDetailsDto
)

/**
 * 一次性提交测试记录请求DTO
 */
data class AnswerSubmitDto(
    val number: Int,
    val audioFileId: String,
    val score: BigDecimal
)

data class ReadingSubmitDto(
    val audioFileId: String,
    val scoreDetails: ReadingScoreDetailsDto
)

data class ExpressionSubmitDto(
    val audioFileId: String,
    val score: BigDecimal,
    val scoreDetails: ExpressionScoreDetailsDto
)

data class SubmitTestRequest(
    val paperId: Long,
    val reading: ReadingSubmitDto,
    val listeningAnswers: List<AnswerSubmitDto>,
    val dialogueAnswers: List<AnswerSubmitDto>,
    val expression: ExpressionSubmitDto
)

/**
 * 练习记录详情响应DTO
 */
data class ExerciseRecordDetailResponse(
    val id: String,
    val userId: Long,
    val paperId: Long,
    val mode: ExerciseMode,
    val status: ExerciseStatus,
    val currentSection: ExerciseStage,
    val progressPercentage: Int,
    val currentQuestionIndex: Int,
    val readingAnswer: ReadingAnswerDto?,
    val listeningAnswers: List<QuestionAnswerDto>,
    val dialogueAnswers: List<QuestionAnswerDto>,
    val expressionAnswer: ExpressionAnswerDto?,
    val totalScore: BigDecimal?,
    val createdAt: LocalDateTime,
    val updatedAt: LocalDateTime,
    val completedAt: LocalDateTime?
)

data class QuestionAnswerDto(
    val number: Int,
    val completed: Boolean,
    val audioFileId: String?,
    val score: BigDecimal?,
    val attemptTime: LocalDateTime?
)

data class ReadingAnswerDto(
    val completed: Boolean,
    val audioFileId: String?,
    val scoreDetails: ReadingScoreDetailsDto?,
    val attemptTime: LocalDateTime?
)

data class ExpressionAnswerDto(
    val completed: Boolean,
    val audioFileId: String?,
    val score: BigDecimal?,
    val scoreDetails: ExpressionScoreDetailsDto?,
    val attemptTime: LocalDateTime?
)

/**
 * 用户练习记录列表响应DTO
 */
data class UserRecordListResponse(
    val records: List<UserRecordSummaryDto>
)

/**
 * 用户练习记录概要DTO
 */
data class UserRecordSummaryDto(
    val recordId: String,
    val paperId: Long,
    val paperTitle: String,
    val mode: ExerciseMode,
    val progressPercentage: Int,
    val totalScore: BigDecimal?,
    val isCompleted: Boolean,
    val createdAt: LocalDateTime,
    val completedAt: LocalDateTime?
)

/**
 * 用户练习统计响应DTO
 */
data class UserStatisticsResponse(
    val totalRecords: Int,
    val practiceRecords: Int,
    val testRecords: Int,
    val completedRecords: Int,
    val averageScore: BigDecimal?
)
